#include "stdafx.h"
#include "CandidateStringClass.h"

BOOL CandidateStringClass::isInit = FALSE;
UINT CandidateStringClass::pageSize = 10;

CandidateStringClass::CandidateStringClass(LPCANDIDATEINFO lpCandInfo)
{
	this->lpCandInfo = lpCandInfo;
	this->lpCandList = (LPCANDIDATELIST)((LPSTR)(this->lpCandInfo)  + this->lpCandInfo->dwOffset[0]);
}

VOID CandidateStringClass::ClearCandidateStr()
{
	this->lpCandList->dwCount = 0;
	this->lpCandList->dwPageStart = 0;
	this->lpCandList->dwSelection = 0;
	this->lpCandList->dwPageSize = pageSize;
}
UINT CandidateStringClass::GetCandidateCount() CONST
{
	return this->lpCandList->dwCount;
}
UINT CandidateStringClass::GetCurrentPageStart() CONST
{
	return this->lpCandList->dwPageStart;
}
UINT CandidateStringClass::GetPageSize() CONST
{
	return this->lpCandList->dwPageSize;
}
VOID CandidateStringClass::Initial()
{
	this->lpCandInfo->dwSize = sizeof(MYCAND);
	this->lpCandInfo->dwCount = 1;
	this->lpCandInfo->dwOffset[0] = (DWORD)(&(((LPMYCAND)this->lpCandInfo)->cl)) - (DWORD)this->lpCandInfo;
	this->lpCandList = (LPCANDIDATELIST)((LPBYTE)(this->lpCandInfo) + this->lpCandInfo->dwOffset[0]);
	this->lpCandList->dwOffset[0] = (DWORD)((LPMYCAND)(this->lpCandInfo))->cands - (DWORD)this->lpCandList;
	this->lpCandList->dwSize = sizeof(MYCAND) - sizeof(CANDIDATEINFO); 
	this->ClearCandidateStr();
}

CANDSTRUCT* CONST CandidateStringClass::GetCode(UINT seletIndex) CONST
{	
	LPMYCAND lpMyCand = (LPMYCAND)this->lpCandInfo;
	seletIndex += this->lpCandList->dwPageStart;
	if(seletIndex >= this->lpCandList->dwCount)
		return NULL;
	else
	{
		return &lpMyCand->cands[seletIndex];
	}
}

VOID CandidateStringClass::SetCandidateCount(UINT count)
{
	if(count < MAXCANDIDATENUM)
	{
		this->ClearCandidateStr();
		this->lpCandList->dwCount = count;
	}
}
VOID CandidateStringClass::SetCandidateStr(CONST LPTSTR lpTxt,BOOL isword,UINT index)
{
	LPMYCAND lpMyCand = (LPMYCAND)this->lpCandInfo;
	if(index < MAXCANDIDATENUM && index < this->lpCandList->dwCount)
	{
		_tcscpy(lpMyCand->cands[index].szCnadStr,lpTxt);
		lpMyCand->cands[index].isCompleteWord = isword;
	}
}

BOOL CandidateStringClass::HavePreviousPage() CONST
{
	return this->lpCandList->dwPageStart > 0;
}
BOOL CandidateStringClass::HaveNextPage() CONST
{
	return this->lpCandList->dwPageStart + this->lpCandList->dwPageSize < this->lpCandList->dwCount - 1;
}
BOOL CandidateStringClass::GoToPreviousPage()
{
	if(this->HavePreviousPage())
	{
		this->lpCandList->dwPageStart -= this->lpCandList->dwPageSize;
		return TRUE;
	}
	return FALSE;
}
BOOL CandidateStringClass::GoToNextPage()
{
	if(this->HaveNextPage())
	{
		this->lpCandList->dwPageStart += this->lpCandList->dwPageSize;
		return TRUE;
	}
	return FALSE;
}